package com.wr.springbootadmin.service.impl;

import com.alibaba.fastjson.JSON;
import com.wr.springbootadmin.conponet.ConfigPrepertiesComponet;
import com.wr.springbootadmin.pojo.*;
import com.wr.springbootadmin.service.DoWorkService;
import com.wr.springbootadmin.service.HikApkService;
import com.wr.springbootadmin.utils.UploadImg;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 王瑞
 * @description: TODO
 * @date 2021/4/20  15:28
 */
@Slf4j
@Service
public class DoWorkServiceImpl implements DoWorkService {

    /*温州市市局平台需要将视屏定时图片数据推送到省厅的点位 一共38个，这里只有37个 有一个没有indexcode
     * dataCode：这个是省厅定川平台给各个小水电站的编号，用于和图片做对应关联
     * indexCode：海康平台下的视屏点位的唯一标识
     *
     * */
    public static final List<WenzhouDataPojo> wenzhouDataPojoList = JSON.parseArray(
            " [\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281061\",\n" +
                    "      \"name\": \"新荣电站\",\n" +
                    "      \"indexCode\": \"33032700581314001099\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303271001\",\n" +
                    "      \"name\": \"桥墩电站\",\n" +
                    "      \"indexCode\": \"33032700581314000977\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811003\",\n" +
                    "      \"name\": \"瑞安市大坪水电站\",\n" +
                    "      \"indexCode\": \"007ec73f72ed4966a0bedfd4cc193077\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811011\",\n" +
                    "      \"name\": \"瑞安市徐山水电站\",\n" +
                    "      \"indexCode\": \"3b1d059b0d1e48a28be823be55ba3c8f\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811018\",\n" +
                    "      \"name\": \"瑞安市桂峰一站\",\n" +
                    "      \"indexCode\": \"d92979432076442490e6135fb4da185c\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811029\",\n" +
                    "      \"name\": \"瑞安市马屿镇（上洞）水电站\",\n" +
                    "      \"indexCode\": \"de5116ac18f54ee0891fa146ee6f4d62\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811035\",\n" +
                    "      \"name\": \"瑞安市罗南水电站\",\n" +
                    "      \"indexCode\": \"13acfb16f2214537b0d805120a2f5682\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303811039\",\n" +
                    "      \"name\": \"瑞安市洪地水电站\",\n" +
                    "      \"indexCode\": \"b8e8ee2b65124a9f92448703c019acb3\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821001\",\n" +
                    "      \"name\": \"乐清市钟前一级水电站\",\n" +
                    "      \"indexCode\": \"06b303a2abf44085b65cf9be7eecdf3c\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821002\",\n" +
                    "      \"name\": \"乐清市钟前二级水电站\",\n" +
                    "      \"indexCode\": \"06b303a2abf44085b65cf9be7eecdf3c\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821039\",\n" +
                    "      \"name\": \"乐清市岭底乡水电站\",\n" +
                    "      \"indexCode\": \"2c58d792534f4c0eb607cd082fde830a\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303291004\",\n" +
                    "      \"name\": \"雪溪水电站\",\n" +
                    "      \"indexCode\": \"33032900001320000069\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281006\",\n" +
                    "      \"name\": \"文成县百丈漈一级水电站\",\n" +
                    "      \"indexCode\": \"80329b48596546828513b64ab4d469a2\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281001\",\n" +
                    "      \"name\": \"文成县百丈漈二级水电站\",\n" +
                    "      \"indexCode\": \"381c51a09b554eb487631a5485bfef3d\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281058\",\n" +
                    "      \"name\": \"文成县支垟三级引水电站\",\n" +
                    "      \"indexCode\": \"ec140b88d53d430a94b2debc56d3b7c4\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281057\",\n" +
                    "      \"name\": \"文成县支垟三级电站\",\n" +
                    "      \"indexCode\": \"605e001c9ea4495797972e93984ff980\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281061\",\n" +
                    "      \"name\": \"文成县高塔电站\",\n" +
                    "      \"indexCode\": \"26a684a2195d468988d8ccf9c83cb8b9\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281081\",\n" +
                    "      \"name\": \"平阳县新田水电站\",\n" +
                    "      \"indexCode\": \"f8bf82e8b19847489f2d240f1e3a0016\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281082\",\n" +
                    "      \"name\": \"文成县银桥电站\",\n" +
                    "      \"indexCode\": \"91b145723b6b4f04b73a7ddcf17ddaf3\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281085\",\n" +
                    "      \"name\": \"文成县东山三级电站\",\n" +
                    "      \"indexCode\": \"816a7904bad74593940db59ac9903bc5\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303271027\",\n" +
                    "      \"name\": \"苍南县长溪源水电站\",\n" +
                    "      \"indexCode\": \"448b3491e5c34263ad809973a98ed9b1\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303271034\",\n" +
                    "      \"name\": \"金星电站\",\n" +
                    "      \"indexCode\": \"33032700581314001093\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821008\",\n" +
                    "      \"name\": \"乐清市华尾水电站\",\n" +
                    "      \"indexCode\": \"179ee74bc335448a9bdbc037381140a8\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821012\",\n" +
                    "      \"name\": \"乐清市白水漈水电站\",\n" +
                    "      \"indexCode\": \"1b96e843966c47ccbfce91b5efac4d01\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821015\",\n" +
                    "      \"name\": \"乐清市屿山水电站\",\n" +
                    "      \"indexCode\": \"5a74acbe31414549bb271d4ec1510c34\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821018\",\n" +
                    "      \"name\": \"乐清市十八玍水电站\",\n" +
                    "      \"indexCode\": \"5c4e435e4efe421190f649fd5d36fb80\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821034\",\n" +
                    "      \"name\": \"乐清市东林水电站（荆津水电站）\",\n" +
                    "      \"indexCode\": \"f7507cfefad54ff78ee7d68e6261520f\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821037\",\n" +
                    "      \"name\": \"乐清市流水岩水电站\",\n" +
                    "      \"indexCode\": \"fedf48b82cc34b03b675e4e99c35e3db\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821041\",\n" +
                    "      \"name\": \"乐清市本觉寺水电站\",\n" +
                    "      \"indexCode\": \"0ec44752eed84988a3e228bea27dd47a\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821046\",\n" +
                    "      \"name\": \"乐清市长龙水电站\",\n" +
                    "      \"indexCode\": \"ad34bba964664de490bb56892c3476ce\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281029\",\n" +
                    "      \"name\": \"文成县东溪五级水电站\",\n" +
                    "      \"indexCode\": \"428d6285e7ea4e2bb9d2a2ab8f319587\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281039\",\n" +
                    "      \"name\": \"文成县福首源水电站\",\n" +
                    "      \"indexCode\": \"c2b6f0e98b9f4a0780c8580c6f96c045\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281050\",\n" +
                    "      \"name\": \"文成县南坑水电站\",\n" +
                    "      \"indexCode\": \"6cf3e989fe494aa6a7f6ee26c486b450\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281068\",\n" +
                    "      \"name\": \"文成县毛坑水电站\",\n" +
                    "      \"indexCode\": \"3c4c571fd8a84f4ea4a479ee20f6d10d\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303281079\",\n" +
                    "      \"name\": \"文成县陈岙水电站\",\n" +
                    "      \"indexCode\": \"55be3361c6ad4e4ca6ed5b4049eabffe\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303241099\",\n" +
                    "      \"name\": \"永嘉县沙头水电站\",\n" +
                    "      \"indexCode\": \"33032436581314000492\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"dataCode\": \"3303821021\",\n" +
                    "      \"name\": \"乐清市散水岩水电站\",\n" +
                    "      \"indexCode\": \"cdfc349469214fc3a270db5a7b610afb\"\n" +
                    "    }\n" +
                    "  ]"
            , WenzhouDataPojo.class);


    @Autowired
    private HikApkService hikApkService;
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /* @Descreption:功能描述:
     * 温州市局定时推图接口
     * @Param
     * @return:
     * @Author: 王瑞
     * @Date: 2021/4/20 15:35
     */
    @Scheduled(cron = "0 25 * * * ?")
    @Override
    public void wenZhouShiJu() throws Exception {
        log.info("温州市局定推图接口触发");
        List<String> collect = wenzhouDataPojoList.stream().map(WenzhouDataPojo::getIndexCode).collect(Collectors.toList());
        List<PicPojo> picPojoList = hikApkService.getImgUrlByIndexCodeList(collect, "21656980", "v0cdY8IQMoOVpairzN89", "61.164.105.28:6443");

        HashSet<PicPojo> PicPojoSet = new HashSet<>(picPojoList);


        System.out.println(JSON.toJSONString(picPojoList));
        List<CangNanPojo> cangNanImgDatalist1 = new ArrayList<>();
        PicPojoSet.forEach(picPojo -> {
            Date parse = null;
            try {
                parse = sdf2.parse(picPojo.getCaptureTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String format1 = sdf.format(parse);

            CangNanPojo cangNanPojo = new CangNanPojo();
            cangNanPojo.setGmtCollect(format1);
            String captureUrl = picPojo.getCaptureUrl();
            String newUrl = captureUrl.replace("http://10.33.158.145:8081", "https://61.164.105.28:6443");
            cangNanPojo.setImgUrl(newUrl);
            cangNanPojo.setName(picPojo.getCameraName());
            if (picPojo.getCameraName().equals("乐清市钟前一级水电站")) {
                CangNanPojo cangNan = new CangNanPojo();
                cangNan.setGmtCollect(format1);
                cangNan.setImgUrl(newUrl);
                cangNan.setName("乐清市钟前二级水电站");
                cangNan.setVideoCode("3303821002");
                cangNanImgDatalist1.add(cangNan);
            }

            if (picPojo.getCameraName().equals("乐清市十八玍水电站")) {
                CangNanPojo cangNan = new CangNanPojo();
                cangNan.setGmtCollect(format1);
                cangNan.setImgUrl(newUrl);
                cangNan.setName("乐清市岭脚水电站");
                cangNan.setVideoCode("3303821018");
                cangNanImgDatalist1.add(cangNan);
            }

            cangNanPojo.setVideoCode(picPojo.getCameraIndexCode());
            cangNanImgDatalist1.add(cangNanPojo);
        });

        wenzhouDataPojoList.forEach(wenzhouDataPojo -> {
            cangNanImgDatalist1.forEach(cangNanPojo -> {
                if (wenzhouDataPojo.getIndexCode().trim().equals(cangNanPojo.getVideoCode())) {
                    cangNanPojo.setVideoCode(wenzhouDataPojo.getDataCode());
                }
            });
        });
        System.out.println("抓到图片：" + cangNanImgDatalist1.size());
        System.out.println(JSON.toJSONString(cangNanImgDatalist1));

        pushImgShiju(cangNanImgDatalist1);

    }

    @Autowired
    private ConfigPrepertiesComponet configPrepertiesComponet;

    @Autowired
    private RestTemplate restTemplate;

    public void pushImgShiju(List<CangNanPojo> cangNanImgDatalist) throws Exception {
        List<StaticImgDataPojo> list = new ArrayList<>();
        Map<String, String> stringStringMap = jsonToMap("hyst_config.json");
        for (int i = 0; i < cangNanImgDatalist.size(); i++) {
            CangNanPojo cangNanPojo = cangNanImgDatalist.get(i);
            String uploadToLocalPath = uploadToShiju(cangNanPojo.getImgUrl(), cangNanPojo.getName(), configPrepertiesComponet.getCangnanImgPath());
            if (!StringUtils.isEmpty(uploadToLocalPath)) {
                toPushImg(uploadToLocalPath, list, cangNanPojo, stringStringMap);
            }
        }
        realTimeDataOfStaticPictures(list, configPrepertiesComponet.getWenzhouUserCode(), configPrepertiesComponet.getWenzhouPassWord());

    }

    public void realTimeDataOfStaticPictures(List<StaticImgDataPojo> list, String userCode, String passWord) {

        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            RealTimeDataPojo realTimeDataPojo = new RealTimeDataPojo();
            realTimeDataPojo.setList(list);
            realTimeDataPojo.setPassWord(passWord);
            realTimeDataPojo.setUserCode(userCode);
            realTimeDataPojo.setRowCount(list.size());
            realTimeDataPojo.setTableCode("videoRecords");
            map.put("rmap", realTimeDataPojo);
            System.out.println(JSON.toJSONString(map));
            HashMap map2 = restTemplate.postForObject(configPrepertiesComponet.getDockingUrlPre(), map, HashMap.class);
            System.out.println(userCode + ":" + map2.get("message"));
        }
    }

    public String uploadToShiju(String imgUrl, String cameraName, String localDirPath) throws NoSuchAlgorithmException, IOException, KeyManagementException {
        long l = System.currentTimeMillis();
        String fileName = l + "-" + cameraName + ".jpg";
        String localPath = UploadImg.downloadBShiJu(imgUrl, fileName, localDirPath);
        return localPath;
    }


    /* @Descreption:功能描述:
     * 宁波市局定时推图接口
     * @Param
     * @return:
     * @Author: 王瑞
     * @Date: 2021/4/20 15:35
     */
    @Override
    public void ningBo() {

    }

    /* @Descreption:功能描述:
     * 温州市苍南定时推图接口
     * @Param
     * @return:
     * @Author: 王瑞
     * @Date: 2021/4/20 15:35
     */
    @Scheduled(cron = "0 25 * * * ?")
    @Override
    public void cangNan() throws Exception {
        log.info("苍南苍南定时推图接口触发");
        List<CangNanPojo> cangNanImgDatalist = getCangNanImgData(configPrepertiesComponet.getCangnanImgUrl());
        pushImg(cangNanImgDatalist);

    }


    @Scheduled(cron = "0 45 * * * ?")
    @Override
    public void toLoadDynamicData() {

        RealTimeDataPojo realTimeDataPojo = new RealTimeDataPojo();

        wenzhouDataPojoList.forEach(item -> {
            List<StaticImgDataPojo> list = new ArrayList<>();
            StaticImgDataPojo staticImgDataPojo = new StaticImgDataPojo();
            staticImgDataPojo.setVideoCode(item.getIndexCode());
            staticImgDataPojo.setHourTime(sdf.format(new Date()));
            Integer hasUpTostd = 1;
            if (item.getIndexCode().equals("cdfc349469214fc3a270db5a7b610afb") || item.getIndexCode().equals("0ec44752eed84988a3e228bea27dd47a")) {
                hasUpTostd = 2;
            }
            staticImgDataPojo.setHasUpToStd(hasUpTostd);
            staticImgDataPojo.setIsOnline(1);
            staticImgDataPojo.setUserCode(configPrepertiesComponet.getWenzhouUserCode());
            staticImgDataPojo.setOp("insert");
            list.add(staticImgDataPojo);
            realTimeDataPojo.setList(list);
            realTimeDataPojo.setTableCode("videoRecords");
            realTimeDataPojo.setRowCount(100);
            realTimeDataPojo.setUserCode(configPrepertiesComponet.getWenzhouUserCode());
            realTimeDataPojo.setPassWord(configPrepertiesComponet.getWenzhouPassWord());


            Map<String, RealTimeDataPojo> map = new HashMap();
            map.put("rmap", realTimeDataPojo);

            ResponseEntity<HashMap> hashMapResponseEntity = restTemplate.postForEntity(this.configPrepertiesComponet.getDockingToLoad(), map, HashMap.class);
            Object body = hashMapResponseEntity.getBody();
            System.out.println("推送完成");


        });
    }

    @SneakyThrows
    @Override
    public void toLoadHistoryDynamicData() {
        RealTimeDataPojo realTimeDataPojo = new RealTimeDataPojo();

        Date startdate= sdf.parse("2021-08-01 00:00:00");
        long initTime = 0l;
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 24; j++) {
                long time = startdate.getTime();
                initTime = i*24*60*60*1000+ j*60*60*1000;
                time+=initTime ;

                Date current = new Date(time);

                String currentTime = sdf.format(current);
                log.info("在推送"+currentTime+"的数据");
                wenzhouDataPojoList.forEach(item -> {
                    List<StaticImgDataPojo> list = new ArrayList<>();
                    StaticImgDataPojo staticImgDataPojo = new StaticImgDataPojo();
                    staticImgDataPojo.setVideoCode(item.getIndexCode());
                    staticImgDataPojo.setHourTime(currentTime);
                    Integer hasUpTostd = 1;
                    if (item.getIndexCode().equals("cdfc349469214fc3a270db5a7b610afb") || item.getIndexCode().equals("0ec44752eed84988a3e228bea27dd47a")) {
                        hasUpTostd = 2;
                    }
                    staticImgDataPojo.setHasUpToStd(hasUpTostd);
                    staticImgDataPojo.setIsOnline(1);
                    staticImgDataPojo.setUserCode(configPrepertiesComponet.getWenzhouUserCode());
                    staticImgDataPojo.setOp("insert");
                    list.add(staticImgDataPojo);
                    realTimeDataPojo.setList(list);
                    realTimeDataPojo.setTableCode("videoRecords");
                    realTimeDataPojo.setRowCount(100);
                    realTimeDataPojo.setUserCode(configPrepertiesComponet.getWenzhouUserCode());
                    realTimeDataPojo.setPassWord(configPrepertiesComponet.getWenzhouPassWord());

                    Map<String, RealTimeDataPojo> map = new HashMap();
                    map.put("rmap", realTimeDataPojo);

                    ResponseEntity<HashMap> hashMapResponseEntity = restTemplate.postForEntity(this.configPrepertiesComponet.getDockingToLoad(), map, HashMap.class);
                    Object body = hashMapResponseEntity.getBody();
                    System.out.println("推送完成");

                });
            }
        }
    }


    public List<CangNanPojo> getCangNanImgData(String imgApi) {

        HashMap map = restTemplate.getForObject(imgApi, HashMap.class);

        Object o = map.get("obj");
        List<CangNanPojo> cangNanPojos = JSON.parseArray(JSON.toJSONString(o), CangNanPojo.class);
        return cangNanPojos;
    }

    public void pushImg(List<CangNanPojo> cangNanImgDatalist) throws Exception {
        List<StaticImgDataPojo> list = new ArrayList<>();
        Map<String, String> stringStringMap = jsonToMap("hyst_config.json");
        for (int i = 0; i < cangNanImgDatalist.size(); i++) {
            CangNanPojo cangNanPojo = cangNanImgDatalist.get(i);
            String name = cangNanPojo.getName();
            if ("金星水电站".equals(name)) {
                cangNanPojo.setVideoCode("3303271034");
            }
            String uploadToLocalPath = uploadToLocal(cangNanPojo.getImgUrl(), cangNanPojo.getName(), configPrepertiesComponet.getCangnanImgPath());
            if (!StringUtils.isEmpty(uploadToLocalPath)) {
                toPushImg(uploadToLocalPath, list, cangNanPojo, stringStringMap);
            }
        }

        realTimeDataOfStaticPictures(list, configPrepertiesComponet.getWenzhouUserCode(), configPrepertiesComponet.getWenzhouPassWord());


    }

    public void toPushImg(String uploadToLocalPath, List<StaticImgDataPojo> list, CangNanPojo cangNanPojo, Map<String, String> stringStringMap) {

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        // 设置请求的格式类型
        headers.setContentType(type);
        FileSystemResource fileSystemResource = new FileSystemResource(uploadToLocalPath);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", fileSystemResource);
        form.add("userCode", configPrepertiesComponet.getWenzhouUserCode());
        form.add("passWord", configPrepertiesComponet.getWenzhouPassWord());
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        try {
            StaticImgDataPojo staticImgDataPojo = new StaticImgDataPojo();
            Map<String, Object> map = new HashMap<>();
            Thread.sleep(300);
            ResponseEntity<HashMap> hashMapResponseEntity = restTemplate.postForEntity(configPrepertiesComponet.getDockingImgUpload(), files, HashMap.class);
            System.out.println("推送完成");
            System.out.println(hashMapResponseEntity.getBody());
            HashMap bodyMap = hashMapResponseEntity.getBody();
            Integer status = (Integer) bodyMap.get("status");
            if (status.equals(0)) {
                HashMap messageMap = (HashMap) bodyMap.get("message");
                Integer fileId = (Integer) messageMap.get("fileId");
                staticImgDataPojo.setFileId(fileId);
                staticImgDataPojo.setUserCode(configPrepertiesComponet.getWenzhouUserCode());
                staticImgDataPojo.setHourTime(cangNanPojo.getGmtCollect());
                staticImgDataPojo.setVideoCode(cangNanPojo.getVideoCode());
                list.add(staticImgDataPojo);
            }
            String gmtCollect = cangNanPojo.getGmtCollect();
            Date parse = sdf3.parse(gmtCollect);
            map.put("PIC_INFO", imgPaeseBase64Str(uploadToLocalPath));
            map.put("REC_TIME", sdf3.format(parse));
            map.put("HYST_CODE", cangNanPojo.getVideoCode());
            map.put("WAIN_NUM", "01");
            String token = stringStringMap.get(cangNanPojo.getVideoCode());
            if (token != null) {
                System.out.println("开始保存图片：" + JSON.toJSONString(cangNanPojo));
                savaImg(cangNanPojo.getVideoCode(), map, token);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            File file = new File(uploadToLocalPath);
            file.delete();
        }

    }

    public String uploadToLocal(String imgUrl, String cameraName, String localDirPath) {
        long l = System.currentTimeMillis();
        String fileName = l + "-" + cameraName + ".jpg";
        String localPath = UploadImg.downloadB(imgUrl, fileName, localDirPath);
        return localPath;
    }

    public String imgPaeseBase64Str(String imgUrl) {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgUrl);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        return encoder.encode(data);

    }


    public void savaImg(String videoCode, Map<String, Object> map, String token) throws IOException {
        String url = "http://61.164.105.24:38060/htwaterAPI/ReportStaticPictureInfo";
        HttpHeaders headers = new HttpHeaders();
        headers.add("AuthorizationKey", videoCode);
        headers.add("AuthorizationToken", token);
        headers.add("Content-Type", "application/json");

        Map<String, Object> parem = new HashMap<>();
        ArrayList<Map> arrayList = new ArrayList<>();
        arrayList.add(map);
        parem.put("DATA", arrayList);
        String body = JSON.toJSONString(parem);
        HttpEntity<String> entity = new HttpEntity(body, headers);
        String result = restTemplate.postForObject(url, entity, String.class);
        System.out.println(result);

    }


    /* @Descreption:功能描述: 将resource下的json文件读取并转换成map
     * 〈〉
     * @Param
     * @return:
     * @Author: 王瑞
     * @Date: 2021/4/21 9:58
     */
    public Map<String, String> jsonToMap(String jsonLocalFileName) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource(jsonLocalFileName);
        InputStream is = classPathResource.getInputStream();
        String result = IOUtils.toString(is, StandardCharsets.UTF_8);
        List<JsonPojo> jsonPojos = JSON.parseArray(result, JsonPojo.class);
        Map<String, String> map = jsonPojos.stream().collect(Collectors.toMap(JsonPojo::getVedioCode, JsonPojo::getToken));
        return map;

    }


}
